#include "BreakPointService.h"
#include <iostream>
#include "BreakPointMem.h"
#include "BreakPointFile.h"


BreakPointService::BreakPointService()
:status(0)
, mem(new BreakPointMem)
, file(new BreakPointFile("./seqNo.bin"))
{
}
BreakPointService::~BreakPointService()
{
    if(file){
        delete file;
    }
    if(mem){
        delete mem;
    }
}






const long long BreakPointService::loadFromFile(){
    std::cout << "BreakPointService::loadFromFile " << std::endl;
    return file->loadFromFile();
}
void BreakPointService::saveToFile(const long long seqNo){
    std::cout << "BreakPointService::saveToFile " << std::endl;
    file->saveToFile(seqNo);
}
void BreakPointService::set(const long long seqNo){
    mem->set(seqNo);
}
const long long BreakPointService::get() const {
    return mem->get();
}

void BreakPointService::start(){
    std::cout << "BreakPointService::start " << std::endl;
    long long seqNo = loadFromFile();
    set(seqNo);
}
void BreakPointService::stop(){
    long long seqNo = get();
    saveToFile(seqNo);
    std::cout << "BreakPointService::stop to: " << seqNo << std::endl;
}
void BreakPointService::receivedNormalResp(const char * msg){
    std::cout << "BreakPointService::receive :[" << msg << "]" << std::endl;
    set(get()+1); //fake action
}
void BreakPointService::timeout(){
    std::cout << "BreakPointService::timeout "
        <<"-> query the data [" <<  get()+1 <<  ", MAX)" << std::endl;

    sendMessage("breakpoint-req");

    saveToFile(get());
}






void BreakPointService::connected(){
    std::cout << "BreakPointService::connected " << std::endl;
    if(status == INITED){
        status = CONNECTED;
    }
    else{
        std::cout << "current status error:"
            << "\n\tstatus :"  << status << " [0-inited,1-connected,2-logined,3-disconnected]" 
            << std::endl;
    }

    sendMessage("login-req");
}
void BreakPointService::disconnected(){
    if(status == CONNECTED || status == LOGINED){
        status = DISCONNECTED;
    }
    else{
        std::cout << "current status error:"
            << "\n\tstatus :"  << status << " [0-inited,1-connected,2-logined,3-disconnected]" 
            << std::endl;
    }
    std::cout << "BreakPointService::disconnected " 
        << "\n\tstatus :"  << status << " [0-inited,1-connected,2-logined,3-disconnected]" 
        << std::endl;
}


void BreakPointService::receivedLoginResp(const char * msg){
    std::cout << "BreakPointService::receive :[" << msg << "]" 
        << "\n\tswitch to [login] status if response ok" 
        << std::endl;

    if(status == CONNECTED){
        status = LOGINED;
    } 
    else{
        std::cout << "current status error:" 
            << "\n\tstatus :"  << status << " [0-inited,1-connected,2-logined,3-disconnected]" 
            << std::endl;
    }
}

void BreakPointService::receivedBreakPointResp(const char * msg){
    std::cout << "BreakPointService::receive :[" << msg << "]" << std::endl;
}

void BreakPointService::sendMessage(const char * msg){
    std::cout << "BreakPointService::send : [" << msg << "] " 
        << "\n\tstatus :"  << status << " [0-inited,1-connected,2-logined,3-disconnected]" 
        << std::endl;
}
